package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.common.Result;
import org.example.entity.*;
import org.example.service.*;
import org.example.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private ProjectReviewService projectReviewService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private ProjectTypeService projectTypeService;
    @Autowired
    private ResourceTypeService resourceTypeService;
    @Autowired
    private JiaoyanmaService jiaoyanmaService;
    @Autowired
    private ImgService imgService;
@Autowired
private ClasstypeService classtypeService;
@Autowired
private CompetitionsService competitionsService;

    @GetMapping("/application")
    public Result application(){
        List<Project> projectList = projectService.list(new QueryWrapper<Project>().orderByAsc("id"));
        return Result.success(projectList);
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        projectReviewService.removeById(id);
        return Result.success();
    }
    @GetMapping("/selectResource2")
    public Result selectResource2(){
        List<Resources> resourcesList = resourceService.list(new QueryWrapper<Resources>().orderByAsc("id"));
        return Result.success(resourcesList);
    }
    @GetMapping("/selectJiaoyanma")
    public Result selectJiaoyanma(){
        List<Jiaoyanma> jiaoyanmaList = jiaoyanmaService.list(new QueryWrapper<Jiaoyanma>().orderByAsc("id"));
        return Result.success(jiaoyanmaList);
    }
    @GetMapping("/selectImg")
    public Result selectImg(){
        List<Img> imgList = imgService.list(new QueryWrapper<Img>().orderByAsc("id"));
        return Result.success(imgList);
    }
    @GetMapping("/selectCompetitions")
    public Result selectCompetitions(){
        List<Competitions> competitionsList = competitionsService.list(new QueryWrapper<Competitions>().orderByAsc("id"));
        return Result.success(competitionsList);
    }
    @PostMapping("/removeResource")
    public Result removeResource(@RequestParam("id") int id){
        resourceService.removeById(id);
        return Result.success();
    }
    @PostMapping("/deleteImg")
    public Result deleteImg(@RequestParam("id") int id){
        imgService.removeById(id);
        return Result.success();
    }
    @PostMapping("/updateCompetition")
    public Result updateCompetition(@RequestParam("id") int id,@RequestBody Competitions competitions){
        competitions.setId(id);
        competitionsService.updateById(competitions);
        return Result.success();
    }
    @PostMapping("/addCompetition")
    public Result updateCompetition(@RequestBody Competitions competitions){
        competitionsService.save(competitions);
        return Result.success();
    }
    @PostMapping("/deleteCompetition")
    public Result deleteCompetition(@RequestParam("id") int id){
        competitionsService.removeById(id);
        return Result.success();
    }
    @PostMapping("/addimg")
    public Result addimg(@RequestBody String imgurl){
        String result = imgurl.replaceAll("\"", "");
        Img img=new Img();
        img.setImgurl(result);
        imgService.save(img);
        return Result.success();
    }
    @PostMapping("/removeJiaoyanma")
    public Result removeJiaoyanma(@RequestParam("id") int id){
        jiaoyanmaService.removeById(id);
        return Result.success();
    }
    @GetMapping("/pingshen")
    public Result pingshen( @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        Page<ProjectReview> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ProjectReview> wrapper = new QueryWrapper<ProjectReview>().orderByAsc("id");

// 执行分页查询（同时包含分页参数和排序条件）
        projectReviewService.page(page, wrapper);

        return Result.success(page);
    }
    @PostMapping("/removePingshen")
    public Result removePingshen(@RequestParam("id") int id){
        projectReviewService.removeById(id);
        return Result.success();
    }

    @PostMapping("/addClass")
    public Result addClass(@RequestParam("classname") String classname){
        ClassType classType=new ClassType();
        classType.setClassname(classname);
        classtypeService.save(classType);
        return Result.success();
    }
    @PostMapping("/addJiaoyanma")
    public Result addJiaoyanma(@RequestParam("Jiaoyanma") String Jiaoyanma){
        Jiaoyanma jiaoyanma2=new Jiaoyanma();
        jiaoyanma2.setMm(Jiaoyanma);
        jiaoyanmaService.save(jiaoyanma2);
        return Result.success();
    }
    @PostMapping("/addProjectType")
    public Result addProjectType(@RequestParam("type") String type){
        Projecttype projecttype=new Projecttype();
        projecttype.setType(type);
        projectTypeService.save(projecttype);
        return Result.success();
    }
    @PostMapping("/addResourceType")
    public Result addResourceType(@RequestParam("resourceType") String resourceType){
        ResourceType resourceType1=new ResourceType();
        resourceType1.setResourceType(resourceType);
        resourceTypeService.save(resourceType1);
        return Result.success();
    }
    @PostMapping("/removeClass")
    public Result removeClass(@RequestParam("id") int id){

        classtypeService.removeById(id);
        return Result.success();
    }
    @PostMapping("/removeProjectType")
    public Result removeProjectType(@RequestParam("id") int id){

        projectTypeService.removeById(id);
        return Result.success();
    }
    @PostMapping("/removeResourceType")
    public Result removeResourceType(@RequestParam("id") int id){

        resourceTypeService.removeById(id);
        return Result.success();
    }
}
